//
//  StringUtil.m
// 

#import "StringUtil.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import <CoreLocation/CLLocationManager.h>
#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h>

@implementation StringUtil

+ (CGSize)textSizeWithText:(NSString *)textStr font:(UIFont*)font numberOfLines:(NSInteger)numberOfLines lineSpacing:(CGFloat)lineSpacing constrainedWidth:(CGFloat)constrainedWidth{
    if (textStr.length == 0) {
        return CGSizeZero;
    }
    CGFloat oneLineHeight = font.lineHeight;
    CGSize textSize = [textStr boundingRectWithSize:CGSizeMake(constrainedWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    CGFloat rows = textSize.height / oneLineHeight;
    CGFloat realHeight = oneLineHeight;
    if (numberOfLines == 0) {
        if (rows >= 1) {
            realHeight = (rows * oneLineHeight) + (rows - 1) * lineSpacing;
        }
    } else {
        if (rows > numberOfLines) {
            rows = numberOfLines;
                }
        realHeight = (rows * oneLineHeight) + (rows - 1) * lineSpacing;
    }
    return CGSizeMake(textSize.width, realHeight);
}

+ (NSAttributedString*)setText:(NSString*)text lineSpacing:(CGFloat)lineSpace font:(UIFont*)font withAlignment:(NSTextAlignment)alignment{
    if (text.length<=0) {
        return nil;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace;
    paragraphStyle.alignment = alignment;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    NSDictionary *attrsDictionary =@{
        NSFontAttributeName: font,
        NSParagraphStyleAttributeName:paragraphStyle
    };
    
    return [[NSAttributedString alloc]initWithString:text attributes:attrsDictionary];
}

+ (NSString* )starsReplacedOfString:(NSString *)str withinRange:(NSRange)range{
    if (str == nil || [str length]< range.location + range.length){
        return str;
    }
    
    NSMutableString* mStr = [[NSMutableString alloc]initWithString:str];
    [mStr replaceCharactersInRange:range withString:[[NSString string] stringByPaddingToLength:range.length withString: @"*" startingAtIndex:0]];
    return mStr;
}

+ (void)telephoneCall:(NSString*)telNo{
    if  (PMStringIsEmpty(telNo)) return ;
    NSString * callPhoneUrl =[NSString stringWithFormat:@"tel://%@",telNo];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhoneUrl] options:@{} completionHandler:^(BOOL success) {}];
}

+ (BOOL)determineWhetherTheAPPOpensTheLocation{
    
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
        return YES;
    }
 
    TipsAlertView *alertV = [[TipsAlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:@"ATM Cash needs to request location permission in order to identify your area and provide loan products and services that are more suitable for you." withCancelStr:@"Cancel" withSureStr:@"Turn On"];
    alertV.clickSureBlock = ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:alertV];
    
    return NO;
}

+ (BOOL)isCanUseCamera{
   
    AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (authStatus == AVAuthorizationStatusDenied){
        
        TipsAlertView *alertV = [[TipsAlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:@"ATM Cash needs to request camera permission to take photos of documents and scan your face to verify your true identity." withCancelStr:@"Cancel" withSureStr:@"Turn On"];
        alertV.clickSureBlock = ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
        };
        [[UIApplication sharedApplication].keyWindow addSubview:alertV];
        
        return NO;
        
    }
    
    return YES;
    
}


@end
